###################################################################################################
###################################################################################################
##
##   Trump, Twitter, and Truth Judgments: The Effects of "Disputed" Tags and  Political Knowledge
##   on the Judged Truthfulness of Election Misinformation
##
##   R Script for Data Analysis
##
###################################################################################################
###################################################################################################

# Loading packages

ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)}

packages <- c("lme4", "lmerTest", "reshape2", "Rmisc", "magrittr", "psych", "lsr", "tidyverse", 
              "ggthemes", "viridis", "grid", "simr", "emmeans", "ggpubr", "performance", "osfr")
ipak(packages)


# Importing data

dat <- read.csv("https://osf.io/download/78zuq/?view_only=567fea549d6b49c2ab9cc6b68ed97b3a")


# Dropping Ps with discrepancies in reported voting decision (n = 12)
dat <- dat %>%
  filter(VoteExclude01 == 0)


# Scoring Political Sophistication/Knowledge

dat <- dat %>%
  mutate(PK_3_01 = case_when(
    (PK_3 == "3") ~ 1,
    (PK_3 == "1") ~ 0,
    (PK_3 == "2") ~ 0,
    (PK_3 == "4") ~ 0))

dat <- dat %>%
  mutate(PK_4_01 = case_when(
    (PK_4 == "2") ~ 1,
    (PK_4 == "1") ~ 0,
    (PK_4 == "3") ~ 0,
    (PK_4 == "4") ~ 0,
    (PK_4 == "5") ~ 0))

dat <- dat %>%
  mutate(PK_5_01 = case_when(
    (PK_5 == "1") ~ 1,
    (PK_5 == "2") ~ 0,
    (PK_5 == "3") ~ 0))
table(dat$PK_5_01)

dat <- dat %>%
  mutate(PK_6_01 = case_when(
    (PK_6 == "2") ~ 1,
    (PK_6 == "1") ~ 0,
    (PK_6 == "3") ~ 0))

dat <- dat %>%
  mutate(PK_7_01 = case_when(
    (PK_7 == "1") ~ 1,
    (PK_7 == "2") ~ 0,
    (PK_7 == "3") ~ 0,
    (PK_7 == "4") ~ 0))

dat <- dat %>%
  mutate(PK_8_01 = case_when(
    (PK_8 == "6") ~ 1,
    (PK_8 == "0") ~ 0,
    (PK_8 == "1") ~ 0,
    (PK_8 == "2") ~ 0,
    (PK_8 == "3") ~ 0,
    (PK_8 == "4") ~ 0,
    (PK_8 == "5") ~ 0,
    (PK_8 == "6") ~ 0,
    (PK_8 == "7") ~ 0,
    (PK_8 == "8") ~ 0,
    (PK_8 == "9") ~ 0,
    (PK_8 == "10") ~ 0,
    (PK_8 == "12") ~ 0,
    (PK_8 == "25") ~ 0,
    (PK_8 == "56") ~ 0,
    (PK_8 == "60") ~ 0,
    (PK_8 == "100") ~ 0))

dat <- dat %>%
  mutate(PK_9_01 = case_when(
    (PK_9 == "2") ~ 1,
    (PK_9 == "1") ~ 0,
    (PK_9 == "3") ~ 0,
    (PK_9 == "4") ~ 0))

dat <- dat %>%
  mutate(PK_10_01 = case_when(
    (PK_10 == "1") ~ 1,
    (PK_10 == "2") ~ 0,
    (PK_10 == "3") ~ 0,
    (PK_10 == "4") ~ 0,
    (PK_10 == "5") ~ 0))

dat <- dat %>%
  rowwise() %>%
  mutate(PolSoph = sum(c(PK_1_01, PK_2_01, PK_3_01, PK_4_01, PK_5_01,
                         PK_6_01, PK_7_01, PK_8_01, PK_9_01, PK_10_01), na.rm = TRUE))


# Scoring WordSum Test (Verbal Ability)

dat <- dat %>%
  mutate(WS_1_01 = case_when(
    (WS_1 == "4") ~ 1,
    (WS_1 == "1") ~ 0,
    (WS_1 == "2") ~ 0,
    (WS_1 == "3") ~ 0,
    (WS_1 == "5") ~ 0))

dat <- dat %>%
  mutate(WS_2_01 = case_when(
    (WS_2 == "5") ~ 1,
    (WS_2 == "1") ~ 0,
    (WS_2 == "2") ~ 0,
    (WS_2 == "3") ~ 0,
    (WS_2 == "4") ~ 0))

dat <- dat %>%
  mutate(WS_3_01 = case_when(
    (WS_3 == "5") ~ 1,
    (WS_3 == "1") ~ 0,
    (WS_3 == "2") ~ 0,
    (WS_3 == "3") ~ 0,
    (WS_3 == "4") ~ 0))

dat <- dat %>%
  mutate(WS_4_01 = case_when(
    (WS_4 == "3") ~ 1,
    (WS_4 == "1") ~ 0,
    (WS_4 == "2") ~ 0,
    (WS_4 == "4") ~ 0,
    (WS_4 == "5") ~ 0))

dat <- dat %>%
  mutate(WS_5_01 = case_when(
    (WS_5 == "1") ~ 1,
    (WS_5 == "2") ~ 0,
    (WS_5 == "3") ~ 0,
    (WS_5 == "4") ~ 0,
    (WS_5 == "5") ~ 0))

dat <- dat %>%
  mutate(WS_6_01 = case_when(
    (WS_6 == "3") ~ 1,
    (WS_6 == "1") ~ 0,
    (WS_6 == "2") ~ 0,
    (WS_6 == "4") ~ 0,
    (WS_6 == "5") ~ 0))

dat <- dat %>%
  mutate(WS_7_01 = case_when(
    (WS_7 == "5") ~ 1,
    (WS_7 == "1") ~ 0,
    (WS_7 == "2") ~ 0,
    (WS_7 == "3") ~ 0,
    (WS_7 == "4") ~ 0))

dat <- dat %>%
  mutate(WS_8_01 = case_when(
    (WS_8 == "4") ~ 1,
    (WS_8 == "1") ~ 0,
    (WS_8 == "2") ~ 0,
    (WS_8 == "3") ~ 0,
    (WS_8 == "5") ~ 0))

dat <- dat %>%
  mutate(WS_9_01 = case_when(
    (WS_9 == "4") ~ 1,
    (WS_9 == "1") ~ 0,
    (WS_9 == "2") ~ 0,
    (WS_9 == "3") ~ 0,
    (WS_9 == "5") ~ 0))

dat <- dat %>%
  mutate(WS_10_01 = case_when(
    (WS_10 == "1") ~ 1,
    (WS_10 == "2") ~ 0,
    (WS_10 == "3") ~ 0,
    (WS_10 == "4") ~ 0,
    (WS_10 == "5") ~ 0))

dat <- dat %>%
  rowwise() %>%
  mutate(WS_Test = sum(c(WS_1_01, WS_2_01, WS_3_01, WS_4_01, WS_5_01,
                         WS_6_01, WS_7_01, WS_8_01, WS_9_01, WS_10_01), na.rm = TRUE))


# Voter Partisanship

dat$VoteDec3 <- factor(dat$VoteDec3,
                       levels = c(1, 2, 3),
                       labels = c("Trump Voters",
                                  "Biden Voters", 
                                  "Third-Party/Non-Voters"))


# Attention manipulation check

xtabs(~ Tweet01 + Tweet_Chk, data = dat)  # 104 Ps failed manipulation check
# 57 Ps failed manip check in control condition
# 47 Ps failed manip check in disclaimer condition

dat <- dat %>%
  mutate(tweetChkPass01 = case_when(
    (Tweet01 == "0" & Tweet_Chk == "1") ~ 0,
    (Tweet01 == "0" & Tweet_Chk == "2") ~ 1,
    (Tweet01 == "0" & Tweet_Chk == "3") ~ 1,
    (Tweet01 == "1" & Tweet_Chk == "1") ~ 1,
    (Tweet01 == "1" & Tweet_Chk == "2") ~ 0,
    (Tweet01 == "1" & Tweet_Chk == "3") ~ 1))
table(dat$tweetChkPass01)


# Chi Sq Test - No differences between conditions for pass/fail manipulation check

dat_ManipCheck <- dat %>%
  dplyr::select(tweetChkPass01, Tweet01) %>%
  drop_na(tweetChkPass01, Tweet01)
table_ManipCheck <- table(dat_ManipCheck)
chisq.test(table_ManipCheck)

sum(table(dat_ManipCheck))  # 1078 Ps total for analyses
# Whether these participants are dropped or included does not change model results
# Reviewers suggested including all participants irrespective of this check


# More data wrangling

dat <- dat %>%
  mutate(TJ_tw1 = case_when(
    (Tweet01 == "0") ~ (Tweet1_C),
    (Tweet01 == "1") ~ (Tweet1_D)))

dat <- dat %>%
  mutate(TJ_tw2 = case_when(
    (Tweet01 == "0") ~ (Tweet2_C),
    (Tweet01 == "1") ~ (Tweet2_D)))

dat <- dat %>%
  mutate(TJ_tw3 = case_when(
    (Tweet01 == "0") ~ (Tweet3_C),
    (Tweet01 == "1") ~ (Tweet3_D)))

dat <- dat %>%
  mutate(TJ_tw4 = case_when(
    (Tweet01 == "0") ~ (Tweet4_C),
    (Tweet01 == "1") ~ (Tweet4_D)))


dat$VtFraud_7_T1 <- dat$Contrib7_w4_1
dat$Contrib7_w4T2_1_REV <- (8 - dat$Contrib7_w4T2_1)  # recorded differently by software; reverse/rescale to -3/+3
dat$VtFraud_7_T2 <- (dat$Contrib7_w4T2_1_REV - 4)
table(dat$VtFraud_7_T1); table(dat$VtFraud_7_T2)   # now both are on -3 to +3 scales


# Converting data frame from wide to long format

dat_longTW <- pivot_longer(dat,
                           cols = c(TJ_tw1, TJ_tw2, TJ_tw3, TJ_tw4),
                           names_to = "Tweet",
                           names_pattern = "_(.*)",
                           values_to = "TruthRating7",
                           values_drop_na = TRUE)
dat_longTW$Tweet <- factor(dat_longTW$Tweet)
dat_longTW$Subject <- factor(dat_longTW$Subject)

dat_longTW <- dat_longTW %>%
  mutate(TweetNum_Labels = case_when(
    (Tweet == "tw1") ~ "Tweet 1",
    (Tweet == "tw2") ~ "Tweet 2",
    (Tweet == "tw3") ~ "Tweet 3",
    (Tweet == "tw4") ~ "Tweet 4"))
dat_longTW$TweetNum_Labels <- factor(dat_longTW$TweetNum_Labels)

dat_longTW2 <- dat_longTW %>%
  drop_na(VoteDec3, TruthRating7, Tweet01, PolSoph, Tweet)


# Density plot - distributions by voting group are very different

fig_1 <- dat_longTW2 %>%
  ggplot(aes(x = TruthRating7, fill = VoteDec3)) +
  geom_density(alpha = 0.50) +
  theme_bw() +
  scale_fill_manual(values=c('#c92424', '#4068ed', 'lightgray')) +
  xlab("Truth Judgments") +
  ylab("Density") +
  guides(fill=guide_legend(title=" ")) +
  labs(subtitle = "\n ") +
  theme(axis.title=element_text(size=16)) +
  theme(axis.title.x = element_text(size = 16, colour = "black", 
                                    margin = margin(t = 12, b = 8)),
        axis.title.y = element_text(size = 16, colour = "black", 
                                    margin = margin(r = 12, l = 8)),
        plot.subtitle = element_text(size = 6, color = "black")) +
  theme(axis.text.x = element_text(size = 14, colour = "black")) +
  theme(axis.text.y = element_text(size = 14, colour = "black")) +
  theme(legend.text = element_text(size = 16, colour = "black")) +
  theme(strip.text.x = element_text(size = 14)) +
  coord_cartesian(xlim=c(1, 7)) +
  scale_x_continuous(breaks = seq(1, 7, by = 1))

ggsave("fig_1_density_plot.tiff", fig_1, width = 10, height = 7, dpi = 200)


  ########################################
 ##   Trump Voters - Truth Judgments   ##
########################################

dat_longTW2_TV <- dat_longTW2 %>%
  filter(VoteDec3 == "Trump Voters")

m_PolSoph_TV <- mean(dat_longTW2_TV$PolSoph)
dat_longTW2_TV$PolSoph_ctr <- (dat_longTW2_TV$PolSoph - m_PolSoph_TV)

dat_longTW2_TV$TruthRating7_f <- factor(dat_longTW2_TV$TruthRating7)
dat_longTW2_TV$Tweet01_EC <- factor(dat_longTW2_TV$Tweet01)

contrasts(dat_longTW2_TV$Tweet01_EC) <- c(-0.5, 0.5)

truth_RI_TV <- lmer(TruthRating7 ~ Tweet01_EC*PolSoph_ctr + (1 | Subject) + (1 | Tweet), data = dat_longTW2_TV)
summary(truth_RI_TV)

emtrends(truth_RI_TV, pairwise ~ Tweet01_EC, var = "PolSoph_ctr", adjust = "none")  # 95% CIs
test(emtrends(truth_RI_TV, pairwise ~ Tweet01_EC, var = "PolSoph_ctr", adjust = "none"))  # p-values

# testing for differences b/n exp. condition by political knowledge (-1 sd, mean, +1 sd)
library(margins)
margins_summary(truth_RI_TV, at = list(PolSoph_ctr = c(-2.170052, 0, 2.170052))) 

# controlling for verbal intelligence (WordSum Test)
truth_RI_TV_wst <- lmer(TruthRating7 ~ Tweet01_EC*PolSoph_ctr + WS_Test +
                          (1 | Subject) + (1 | Tweet), data = dat_longTW2_TV)
summary(truth_RI_TV_wst)

# Using pre-existing belief as a predictor
m_Contrib7_w4_1 <- mean(dat_longTW2_TV$Contrib7_w4_1)
dat_longTW2_TV$Contrib7_w4_1_ctr <- (dat_longTW2_TV$Contrib7_w4_1 - m_Contrib7_w4_1)

truth_RI_TV_fraudbelief <- lmer(TruthRating7 ~ Tweet01_EC*Contrib7_w4_1_ctr + 
                                  (1 | Subject) + (1 | Tweet), data = dat_longTW2_TV)
summary(truth_RI_TV_fraudbelief)

emtrends(truth_RI_TV_fraudbelief, pairwise ~ Tweet01_EC, var = "Contrib7_w4_1_ctr", adjust = "none")  # 95% CIs
test(emtrends(truth_RI_TV_fraudbelief, pairwise ~ Tweet01_EC, var = "Contrib7_w4_1_ctr", adjust = "none"))  # p-values

# testing for differences b/n exp. condition by pre-existing belief (-1 sd, mean, max)
margins_summary(truth_RI_TV_fraudbelief, at = list(Contrib7_w4_1_ctr = c(-1.241608, 0, 0.820689655172414))) 


  ########################################
 ##   Biden Voters - Truth Judgments   ##
########################################

dat_longTW2_BV <- dat_longTW2 %>%
  filter(VoteDec3 == "Biden Voters")

m_PolSoph_BV <- mean(dat_longTW2_BV$PolSoph)
dat_longTW2_BV$PolSoph_ctr <- (dat_longTW2_BV$PolSoph - m_PolSoph_BV)

dat_longTW2_BV$Tweet01_EC <- factor(dat_longTW2_BV$Tweet01)
contrasts(dat_longTW2_BV$Tweet01_EC) <- c(-0.5, 0.5)

truth_RI_BV <- lmer(TruthRating7 ~ Tweet01_EC*PolSoph_ctr + (1 | Subject) + (1 | Tweet), data = dat_longTW2_BV)
summary(truth_RI_BV)

# controlling for verbal intelligence (WordSum Test)
truth_RI_BV_wst <- lmer(TruthRating7 ~ Tweet01_EC*PolSoph_ctr + WS_Test +
                          (1 | Subject) + (1 | Tweet), data = dat_longTW2_BV)
summary(truth_RI_BV_wst)

# Using pre-existing belief as a predictor
m_Contrib7_w4_1 <- mean(dat_longTW2_BV$Contrib7_w4_1)
dat_longTW2_BV$Contrib7_w4_1_ctr <- (dat_longTW2_BV$Contrib7_w4_1 - m_Contrib7_w4_1)

truth_RI_BV_fraudbelief <- lmer(TruthRating7 ~ Tweet01_EC*Contrib7_w4_1_ctr + 
                                  (1 | Subject) + (1 | Tweet), data = dat_longTW2_BV)
summary(truth_RI_BV_fraudbelief)


  #########################################
 ##   TP/Non-voters - Truth Judgments   ##
#########################################

dat_longTW2_TPNV <- dat_longTW2 %>%
  filter(VoteDec3 == "Third-Party/Non-Voters")

m_PolSoph_TPNV <- mean(dat_longTW2_TPNV$PolSoph)
dat_longTW2_TPNV$PolSoph_ctr <- (dat_longTW2_TPNV$PolSoph - m_PolSoph_TPNV)

dat_longTW2_TPNV$Tweet01_EC <- factor(dat_longTW2_TPNV$Tweet01)
contrasts(dat_longTW2_TPNV$Tweet01_EC) <- c(-0.5, 0.5)

truth_RI_TPNV <- lmer(TruthRating7 ~ Tweet01_EC*PolSoph_ctr + (1 | Subject) + (1 | Tweet), data = dat_longTW2_TPNV)
summary(truth_RI_TPNV)

# controlling for verbal intelligence (WordSum Test)
truth_RI_TPNV_wst <- lmer(TruthRating7 ~ Tweet01_EC*PolSoph_ctr + WS_Test +
                            (1 | Subject) + (1 | Tweet), data = dat_longTW2_TPNV)
summary(truth_RI_TPNV_wst)

# Using pre-existing belief as a predictor
m_Contrib7_w4_1 <- mean(dat_longTW2_TPNV$Contrib7_w4_1)
dat_longTW2_TPNV$Contrib7_w4_1_ctr <- (dat_longTW2_TPNV$Contrib7_w4_1 - m_Contrib7_w4_1)

truth_RI_TPNV_fraudbelief <- lmer(TruthRating7 ~ Tweet01_EC*Contrib7_w4_1_ctr + 
                                    (1 | Subject) + (1 | Tweet), data = dat_longTW2_TPNV)
summary(truth_RI_TPNV_fraudbelief)


####
# Figures

dat_longTW2 <- dat_longTW2 %>%
  mutate(Tweet01_short = case_when(
    (Tweet01 == "0") ~ "Control",
    (Tweet01 == "1") ~ "Disputed Tag"))

fig_2 <- dat_longTW2 %>%
  drop_na(VoteDec3, PolSoph, Tweet01_short, TruthRating7) %>%
  ggplot(aes(x = PolSoph, y = TruthRating7, color = Tweet01_short)) +
  geom_smooth(linewidth = 2, alpha = 0.15, method = "lm") +
  scale_color_manual(values=c('#4068ed','#c92424')) +
  xlab("Political Knowledge") +
  ylab("Judged Truthfulness") +
  guides(color=guide_legend(title=" ")) +
  labs(subtitle = " ") +
  facet_grid(~ VoteDec3) +
  theme_bw() +
  theme(axis.title=element_text(size=16)) +
  theme(axis.title.x = element_text(size = 16, colour = "black", 
                                    margin = margin(t = 12, b = 8)),
        axis.title.y = element_text(size = 16, colour = "black", 
                                    margin = margin(r = 12, l = 8)),
        plot.subtitle = element_text(size = 6, color = "black")) +
  theme(axis.text.x = element_text(size = 14, colour = "black")) +
  theme(axis.text.y = element_text(size = 14, colour = "black")) +
  theme(legend.text = element_text(size = 16, colour = "black")) +
  theme(strip.text.x = element_text(size = 14)) +
  coord_cartesian(xlim=c(0, 10)) +
  coord_cartesian(ylim=c(1, 7)) +
  scale_y_continuous(breaks = seq(1, 7, by = 1)) +
  scale_x_continuous(breaks = seq(0, 10, by = 2)) 
fig_2

ggsave("fig_2_truth_judgments.tiff", fig_2, width = 10, height = 7, dpi = 200)


fig_3 <- dat_longTW2 %>%
  drop_na(VoteDec3, Contrib7_w4_1, Tweet01_short, TruthRating7) %>%
  ggplot(aes(x = Contrib7_w4_1, y = TruthRating7, color = Tweet01_short)) +
  geom_smooth(linewidth = 2, alpha = 0.15, method = "lm") +
  scale_color_manual(values=c('#4068ed','#c92424')) +
  xlab("Pre-existing Belief in Election Fraud") +
  ylab("Judged Truthfulness") +
  guides(color=guide_legend(title=" ")) +
  labs(subtitle = " ") +
  facet_grid(~ VoteDec3) +
  theme_bw() +
  theme(axis.title=element_text(size=16)) +
  theme(axis.title.x = element_text(size = 16, colour = "black", 
                                    margin = margin(t = 12, b = 8)),
        axis.title.y = element_text(size = 16, colour = "black", 
                                    margin = margin(r = 12, l = 8)),
        plot.subtitle = element_text(size = 6, color = "black")) +
  theme(axis.text.x = element_text(size = 14, colour = "black")) +
  theme(axis.text.y = element_text(size = 14, colour = "black")) +
  theme(legend.text = element_text(size = 16, colour = "black")) +
  theme(strip.text.x = element_text(size = 14)) +
  coord_cartesian(xlim=c(-3, 3)) +
  coord_cartesian(ylim=c(1, 7)) +
  scale_y_continuous(breaks = seq(1, 7, by = 1)) +
  scale_x_continuous(breaks = seq(-3, 3, by = 1))
fig_3

ggsave("fig_3_truth_judgments.tiff", fig_3, width = 10, height = 7, dpi = 200)


  #############################################
 ##   Change in Election Fraud / Fairness   ##
#############################################

# Converting data frame from wide to long format
dat_longCh <- pivot_longer(dat,
                           cols = c(VtFraud_7_T1, VtFraud_7_T2),
                           names_to = "Time",
                           values_to = "Fraud_Est7",
                           values_drop_na = TRUE)

dat_longCh <- dat_longCh %>%
  mutate(Time_PrePost = case_when(
    Time == "VtFraud_7_T1" ~ "Pre",
    Time == "VtFraud_7_T2" ~ "Post"))

dat_longCh$Time_PrePost <- factor(dat_longCh$Time_PrePost)
dat_longCh$Subject <- factor(dat_longCh$Subject)

dat_longCh <- dat_longCh %>%
  mutate(FreeFair_Est7 = case_when(
    Time_PrePost == "Pre" ~ FreeFair,
    Time_PrePost == "Post" ~ FreeFair_T2))

dat_longCh$Tweet01_EC <- factor(dat_longCh$Tweet01)
dat_longCh$Time_PrePost_EC <- factor(dat_longCh$Time_PrePost)
contrasts(dat_longCh$Tweet01_EC) <- c(-0.5, 0.5)
contrasts(dat_longCh$Time_PrePost_EC) <- c(0.5, -0.5)


## Trump Voters - Fraud Belief Change
dat_longCh_TV <- dat_longCh %>%
  filter(VoteDec3 == "Trump Voters")

m_PolSoph_TV <- mean(dat_longCh_TV$PolSoph)
dat_longCh_TV$PolSoph_ctr <- (dat_longCh_TV$PolSoph - m_PolSoph_TV)

fraudCH_RI_TV <- lmer(Fraud_Est7 ~ Time_PrePost_EC*Tweet01_EC + (1 | Subject), data = dat_longCh_TV)
summary(fraudCH_RI_TV)

# Including political knowledge
fraudCH_3Int_RI_TV <- lmer(Fraud_Est7 ~ Time_PrePost_EC*Tweet01_EC*PolSoph_ctr + 
                             (1 | Subject), data = dat_longCh_TV)
summary(fraudCH_3Int_RI_TV)

emtrends(fraudCH_3Int_RI_TV, pairwise ~ Time_PrePost_EC*Tweet01_EC, var = "PolSoph_ctr", adjust = "none")  # 95% CIs
test(emtrends(fraudCH_3Int_RI_TV, pairwise ~ Time_PrePost_EC*Tweet01_EC, var = "PolSoph_ctr", adjust = "none"))  # p-values

# Change in fairness beliefs - Trump voters
fairCH_RI_TV <- lmer(FreeFair_Est7 ~ Time_PrePost_EC*Tweet01_EC + (1 | Subject), data = dat_longCh_TV)
summary(fairCH_RI_TV)

# Including political knowledge
fairCH_3Int_RI_TV <- lmer(FreeFair_Est7 ~ Time_PrePost_EC*Tweet01_EC*PolSoph_ctr + 
                            (1 | Subject), data = dat_longCh_TV)
summary(fairCH_3Int_RI_TV)

emtrends(fairCH_3Int_RI_TV, pairwise ~ Time_PrePost_EC*Tweet01_EC, var = "PolSoph_ctr")  # 95% CIs
test(emtrends(fairCH_3Int_RI_TV, pairwise ~ Time_PrePost_EC*Tweet01_EC, var = "PolSoph_ctr"))  # p-values

mean(dat_longCh_TV$PolSoph_ctr)
sd(dat_longCh_TV$PolSoph_ctr)

dat_longCh_TV_ctrl <- dat_longCh_TV %>%
  filter(Tweet01 == "0")
fairCH_TV_control <- lmer(FreeFair_Est7 ~ Time_PrePost_EC*PolSoph_ctr + 
                            (1 | Subject), data = dat_longCh_TV_ctrl)
summary(fairCH_TV_control)
margins_summary(fairCH_TV_control, at = list(PolSoph_ctr = c(-2.170989, 0, 2.170989))) 

dat_longCh_TV_ctrl_exp <- dat_longCh_TV %>%
  filter(Tweet01 == "1")
fairCH_TV_exp <- lmer(FreeFair_Est7 ~ Time_PrePost_EC*PolSoph_ctr + 
                        (1 | Subject), data = dat_longCh_TV_ctrl_exp)
summary(fairCH_TV_exp)
margins_summary(fairCH_TV_exp, at = list(PolSoph_ctr = c(-2.170989, 0, 2.170989))) 


## Biden Voters - Fraud Belief Change
dat_longCh_BV <- dat_longCh %>%
  filter(VoteDec3 == "Biden Voters")

m_PolSoph_BV <- mean(dat_longCh_BV$PolSoph)
dat_longCh_BV$PolSoph_ctr <- (dat_longCh_BV$PolSoph - m_PolSoph_BV)

fraudCH_RI_BV <- lmer(Fraud_Est7 ~ Time_PrePost_EC*Tweet01_EC + (1 | Subject), data = dat_longCh_BV)
summary(fraudCH_RI_BV)

# Including political knowledge
fraudCH_3Int_RI_BV <- lmer(Fraud_Est7 ~ Time_PrePost_EC*Tweet01_EC*PolSoph_ctr + 
                             (1 | Subject), data = dat_longCh_BV)
summary(fraudCH_3Int_RI_BV)

# change in fairness beliefs - Biden voters
fairCH_RI_BV <- lmer(FreeFair_Est7 ~ Time_PrePost_EC*Tweet01_EC + (1 | Subject), data = dat_longCh_BV)
summary(fairCH_RI_BV)

pairs(emmeans(fairCH_RI_BV, pairwise ~ Time_PrePost_EC | Tweet01_EC))  # p-values
confint(pairs(emmeans(fairCH_RI_BV, pairwise ~ Time_PrePost_EC | Tweet01_EC))) # 95% CIs

# Including political knowledge
fairCH_3Int_RI_BV <- lmer(FreeFair_Est7 ~ Time_PrePost_EC*Tweet01_EC*PolSoph_ctr + (1 | Subject), data = dat_longCh_BV)
summary(fairCH_3Int_RI_BV)


## Third-Party and Non-Voters - Fraud Belief Change
dat_longCh_TPNV <- dat_longCh %>%
  filter(VoteDec3 == "Third-Party/Non-Voters")

m_PolSoph_TPNV <- mean(dat_longCh_TPNV$PolSoph)
dat_longCh_TPNV$PolSoph_ctr <- (dat_longCh_TPNV$PolSoph - m_PolSoph_TPNV)

fraudCH_RI_TPNV <- lmer(Fraud_Est7 ~ Time_PrePost_EC*Tweet01_EC + (1 | Subject), data = dat_longCh_TPNV)
summary(fraudCH_RI_TPNV)

emmeans(fraudCH_RI_TPNV, pairwise ~ Time_PrePost_EC | Tweet01_EC, adjust = "none")  # 95% CIs
pairs(emmeans(fraudCH_RI_TPNV, pairwise ~ Time_PrePost_EC | Tweet01_EC, adjust = "none"))

# Including political knowledge
fraudCH_3Int_RI_TPNV <- lmer(Fraud_Est7 ~ Time_PrePost_EC*Tweet01_EC*PolSoph_ctr + 
                               (1 | Subject), data = dat_longCh_TPNV)
summary(fraudCH_3Int_RI_TPNV)

emtrends(fraudCH_3Int_RI_TPNV, pairwise ~ Time_PrePost_EC*Tweet01_EC, var = "PolSoph_ctr", adjust = "none")  # 95% CIs
test(emtrends(fraudCH_3Int_RI_TPNV, pairwise ~ Time_PrePost_EC*Tweet01_EC, var = "PolSoph_ctr", adjust = "none"))  # p-values

emmeans(fraudCH_3Int_RI_TPNV, pairwise ~ Time_PrePost_EC | Tweet01_EC, adjust = "none")  # 95% CIs
pairs(emmeans(fraudCH_3Int_RI_TPNV, pairwise ~ Time_PrePost_EC | Tweet01_EC, adjust = "none"))

# Change in fairness beliefs - third-party and non-voters
fairCH_RI_TPNV <- lmer(FreeFair_Est7 ~ Time_PrePost_EC*Tweet01_EC + (1 | Subject), data = dat_longCh_TPNV)
summary(fairCH_RI_TPNV)

# Including political knowledge
fairCH_3Int_RI_TPNV <- lmer(FreeFair_Est7 ~ Time_PrePost_EC*Tweet01_EC*PolSoph_ctr + (1 | Subject), data = dat_longCh_TPNV)
summary(fairCH_3Int_RI_TPNV)

mean(dat_longCh_TPNV$PolSoph_ctr)
sd(dat_longCh_TPNV$PolSoph_ctr)

dat_longCh_TPNV_ctrl <- dat_longCh_TPNV %>%
  filter(Tweet01 == "0")
fairCH_TPNV_control <- lmer(FreeFair_Est7 ~ Time_PrePost_EC*PolSoph_ctr + 
                              (1 | Subject), data = dat_longCh_TPNV_ctrl)
summary(fairCH_TPNV_control)
margins_summary(fairCH_TPNV_control, at = list(PolSoph_ctr = c(-2.54425, 0, 2.54425))) 

dat_longCh_TPNV_exp <- dat_longCh_TPNV %>%
  filter(Tweet01 == "1")
fairCH_TPNV_exp <- lmer(FreeFair_Est7 ~ Time_PrePost_EC*PolSoph_ctr + 
                          (1 | Subject), data = dat_longCh_TPNV_exp)
summary(fairCH_TPNV_exp)
margins_summary(fairCH_TPNV_exp, at = list(PolSoph_ctr = c(-2.54425, 0, 2.54425))) 


####
# Figures

dat_longCh <- dat_longCh %>%
  mutate(Tweet01_short = case_when(
    (Tweet01 == "0") ~ "Control",
    (Tweet01 == "1") ~ "Disputed Tag"))

appendix_A_fig_1 <- dat_longCh %>%
  drop_na(VoteDec3, PolSoph, Tweet01_short, Fraud_Est7, Time_PrePost) %>%
  ggplot(aes(x = PolSoph, y = Fraud_Est7, color = Tweet01_short, linetype = Time_PrePost)) +
  geom_smooth(linewidth = 2, alpha = 0.15, method = "lm") +
  scale_color_manual(values=c('#4068ed','#c92424')) +
  xlab("Political Knowledge") +
  ylab("Belief in Election Fraud") +
  guides(color=guide_legend(title="Condition")) +
  guides(linetype=guide_legend(title="Time")) +
  labs(subtitle = " ") +
  facet_grid(~ VoteDec3) +
  theme_bw() +
  theme(axis.title=element_text(size=16)) +
  theme(axis.title.x = element_text(size = 16, colour = "black", 
                                    margin = margin(t = 12, b = 8)),
        axis.title.y = element_text(size = 16, colour = "black", 
                                    margin = margin(r = 12, l = 8)),
        plot.subtitle = element_text(size = 6, color = "black")) +
  theme(axis.text.x = element_text(size = 14, colour = "black")) +
  theme(axis.text.y = element_text(size = 14, colour = "black")) +
  theme(legend.text = element_text(size = 16, colour = "black")) +
  theme(legend.title = element_text(size = 16, colour = "black")) +
  theme(strip.text.x = element_text(size = 14)) +
  coord_cartesian(xlim=c(0, 10)) +
  coord_cartesian(ylim=c(-1, 3)) +
  scale_y_continuous(breaks = seq(-1, 3, by = 1)) +
  scale_x_continuous(breaks = seq(0, 10, by = 2)) 
appendix_A_fig_1

ggsave("append_A_fig_1_fraud_belief.tiff", appendix_A_fig_1, width = 10, height = 7, dpi = 200)


appendix_B_fig_1 <- dat_longCh %>%
  drop_na(VoteDec3, PolSoph, Tweet01_short, FreeFair_Est7, Time_PrePost) %>%
  ggplot(aes(x = PolSoph, y = FreeFair_Est7, color = Tweet01_short, linetype = Time_PrePost)) +
  geom_smooth(linewidth = 2, alpha = 0.15, method = "lm") +
  scale_color_manual(values=c('#4068ed','#c92424')) +
  xlab("Political Knowledge") +
  ylab("Belief in Fair Election") +
  guides(color=guide_legend(title="Condition")) +
  guides(linetype=guide_legend(title="Time")) +
  labs(subtitle = " ") +
  facet_grid(~ VoteDec3) +
  theme_bw() +
  theme(axis.title=element_text(size=16)) +
  theme(axis.title.x = element_text(size = 16, colour = "black", 
                                    margin = margin(t = 12, b = 8)),
        axis.title.y = element_text(size = 16, colour = "black", 
                                    margin = margin(r = 12, l = 8)),
        plot.subtitle = element_text(size = 6, color = "black")) +
  theme(axis.text.x = element_text(size = 14, colour = "black")) +
  theme(axis.text.y = element_text(size = 14, colour = "black")) +
  theme(legend.text = element_text(size = 16, colour = "black")) +
  theme(legend.title = element_text(size = 16, colour = "black")) +
  theme(strip.text.x = element_text(size = 14)) +
  coord_cartesian(xlim=c(0, 10)) +
  coord_cartesian(ylim=c(1, 7)) +
  scale_y_continuous(breaks = seq(1, 7, by = 1)) +
  scale_x_continuous(breaks = seq(0, 10, by = 2)) 
appendix_B_fig_1

ggsave("append_B_fig_1_fair_belief.tiff", appendix_B_fig_1, width = 10, height = 7, dpi = 200)


  ###################################################
 ##   ANOVAs - Differences between voter groups   ##
###################################################

library(afex)

# Fraud beliefs time 1
anova_fraud_t1 <- aov_car(VtFraud_7_T1 ~ VoteDec3 + Error(Subject), data = dat, anova_table = list(es = "pes"))
summary(anova_fraud_t1)

psych::describeBy(dat$VtFraud_7_T1, dat$VoteDec3)
emm_fraud_t1 <- emmeans(anova_fraud_t1, ~ VoteDec3)
pairs(emm_fraud_t1, adjust="none")
confint(pairs(emm_fraud_t1, adjust="none"))

# Fraud beliefs time 2
anova_fraud_t2 <- aov_car(VtFraud_7_T2 ~ VoteDec3 + Error(Subject), data = dat, anova_table = list(es = "pes"))
summary(anova_fraud_t2)

psych::describeBy(dat$VtFraud_7_T2, dat$VoteDec3)
emm_fraud_t2 <- emmeans(anova_fraud_t2, ~ VoteDec3)
pairs(emm_fraud_t2, adjust="none")
confint(pairs(emm_fraud_t2, adjust="none"))

# Fairness beliefs time 1
anova_fair_t1 <- aov_car(FreeFair ~ VoteDec3 + Error(Subject), data = dat, anova_table = list(es = "pes"))
summary(anova_fair_t1)

psych::describeBy(dat$FreeFair, dat$VoteDec3)
emm_fair_t1 <- emmeans(anova_fair_t1, ~ VoteDec3)
pairs(emm_fair_t1, adjust="none")
confint(pairs(emm_fair_t1, adjust="none"))

# Fairness beliefs time 2
anova_fair_t2 <- aov_car(FreeFair_T2 ~ VoteDec3 + Error(Subject), data = dat, anova_table = list(es = "pes"))
summary(anova_fair_t2)

psych::describeBy(dat$FreeFair_T2, dat$VoteDec3)
emm_fair_t2 <- emmeans(anova_fair_t2, ~ VoteDec3)
pairs(emm_fair_t2, adjust="none")
confint(pairs(emm_fair_t2, adjust="none"))


